﻿
namespace SZJT.Framework.Services
{
    public static class ServiceExceptionHelper
    {
        #region 通用异常处理
        private static ServiceException _unkownException = new ServiceException(100, "发生未知错误。");
        private static ServiceException _taskGuidNullException = new ServiceException(99, "TaskGuid不能为空。");
        private static ServiceException _dataGuidNullException = new ServiceException(98, "DataGuid不能为空。");
        private static ServiceException _dataTypeNullException = new ServiceException(97, "DataType不能为空。");
        private static ServiceException _notAuthorizedException = new ServiceException(96, "权限错误。无权限或未登录。");
        private static ServiceException _taskGuidNotDefinedException = new ServiceException(95, "TaskGuid错误。");
        private static ServiceException _taskGuidNotIdenticalErrorException = new ServiceException(94, "TaskGuid不一致。");
        private static ServiceException _dataGuidNotIdenticalErrorException = new ServiceException(93, "DataGuid不一致。");
        private static ServiceException _dataTypeNotIdenticalErrorException = new ServiceException(92, "DataType不一致。");
        private static ServiceException _dataTypeErrorException = new ServiceException(91, "DataType错误。");
        private static ServiceException _schemaErrorException = new ServiceException(90, "数据架构错误。");
        private static ServiceException _foreignKeyException = new ServiceException(89, "外键约束错误。");
        private static ServiceException _projectFlowStatusException = new ServiceException(88, "流程状态不合法。");
        private static ServiceException _projectFlowTaskException = new ServiceException(87, "流程阶段不合法");
      


        public static ServiceException UnkownException
        {
            get
            {
                return _unkownException;
            }
        }

        public static ServiceException ForeignKeyException
        {
            get
            {
                return _foreignKeyException;
            }
        }

        public static void ThrowUnkownException()
        {
            throw _unkownException;
        }

        public static void ThrowTaskGuidNullException()
        {
            throw _taskGuidNullException;
        }

        public static void ThrowDataGuidNullException()
        {
            throw _dataGuidNullException;
        }

        public static void ThrowDataTypeNullException()
        {
            throw _dataTypeNullException;
        }

        public static void ThrowNotAuthorizedException()
        {
            throw _notAuthorizedException;
        }

        public static void ThrowTaskGuidNotDefinedException()
        {
            throw _taskGuidNotDefinedException;
        }

        public static void ThrowTaskGuidNotIdenticalErrorException()
        {
            throw _taskGuidNotIdenticalErrorException;
        }

        

        public static void ThrowDataGuidNotIdenticalErrorException()
        {
            throw _dataGuidNotIdenticalErrorException;
        }

        public static void ThrowDataTypeNotIdenticalErrorException()
        {
            throw _dataTypeNotIdenticalErrorException;
        }

        public static void ThrowDataTypeErrorException()
        {
            throw _dataTypeErrorException;
        }

        public static void ThrowSchemaErrorException()
        {
            throw _schemaErrorException;
        }

        public static void ThrowProjectFlowStatusException()
        {
            throw _projectFlowStatusException;
        }

        public static void ThrowProjectFlowTaskException()
        {
            throw _projectFlowTaskException;
        }

   
        public static void ThrowQueryCriteriaErrorException(string message)
        {
            throw new ServiceException(86, string.Format("查询条件错误。 {0}", message)); ;
        }

        public static void ThrowQueryElementTypeException(string message)
        {
            throw new ServiceException(95, string.Format("查询字段不能为BINARY类型。错误字段是{0}", message));
        }

        /// <summary>
        /// 抛出异常代码
        /// </summary>
        /// <param name="code"></param>
        public static void ThrowCodeException(int code)
        {
            throw new ServiceException(code,"");
        }

        //public static void ThrowEntityInvalidException(Csla.Validation.BrokenRulesCollection brokenRules)
        //{
        //    string message = "非法字段值。";
        //    foreach (var brokenRule in brokenRules)
        //    {
        //        message += brokenRule.Description;
        //    }

        //    throw new ServiceException(84, message);
        //}

        public static void ThrowTypeException(string value, ServiceDataType serviceDataType)
        {
            if (serviceDataType != ServiceDataType.UNKNOWN)
            {
                throw new ServiceException(83, string.Format("类型错误。 {0}不能转换为{1}.", value, serviceDataType.ToString()));
            }
            else
            {
                throw new ServiceException(83, "未知类型。");
            }
        }

        public static void ThrowXmlErrorException(string message)
        {
            throw new ServiceException(83, string.Format("Xml格式错误。{0}", message));
        }

        #endregion

        #region 批量操作时异常处理
        public static void ThrowBatchDeleteException()
        {
            throw new ServiceException(-101, "批量删除时发生异常");
        }
        public static void ThrowBatchSaveException()
        {
            throw new ServiceException(-100, "批量保存时发生异常");
        }
        #endregion    
    }
}
